<?php
class user {
function encrypt($string,$cipherKey='') {
	
   srand((double) microtime() * 1000000); //for sake of MCRYPT_RAND
   $key = md5($cipherKey); //to improve variance
  /* Open module, and create IV */
  $td = mcrypt_module_open('rijndael-256', '','ofb', '');
  $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  $iv_size = mcrypt_enc_get_iv_size($td);
  $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
  /* Initialize encryption handle */
   if (mcrypt_generic_init($td, $key, $iv) != -1) {

      /* Encrypt data */
      $c_t = mcrypt_generic($td, $string);
      //$c_t=base64_encode($c_t); // added
	  mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
       $c_t = $iv.$c_t;
       return base64_encode($c_t);
   } //end if
}

function decrypt($string,$cipherKey='') {
 
  $string=base64_decode($string);
  $key = md5($cipherKey); //to improve variance
  /* Open module, and create IV */
  $td = mcrypt_module_open('rijndael-256', '','ofb', '');
  $key = substr($key, 0, mcrypt_enc_get_key_size($td));
  $iv_size = mcrypt_enc_get_iv_size($td);
  $iv = substr($string,0,$iv_size);
  //$s=base64_decode($string);
  $string = substr($string,$iv_size);
  /* Initialize encryption handle */
   if (mcrypt_generic_init($td, $key, $iv) != -1) {

      /* Encrypt data */
      $c_t = mdecrypt_generic($td,$string);
      mcrypt_generic_deinit($td);
      mcrypt_module_close($td);
       return $c_t;
   } //end if
}	
	function init_user($id) {
		$data=new database();	
		$data->db(DATABASE)->collection('users');
		$data->criteria(array('_id'=>new MongoId($id)));
		$row=$data->findOne();
		$_SESSION['user_id']				=	(string)$row['_id'];
		$_SESSION['username']				=	$row['username'];
		$_SESSION['gravatar']				=	$row['gravatar'];
		$_SESSION['last_login']				=	$row['last_login'];
		$_SESSION['admin']					=	$row['admin'];
		$_SESSION['groups']					=	$row['groups'];
		$_SESSION['logged_in']				=	true;
		$_SESSION['user']['profile']		=	$row['user']['profile']; // set as array  // basic ?
		$_SESSION['user']['personal']		=	$row['user']['personal'];
		$_SESSION['user']['physical']		= 	$row['user']['physical'];
		$_SESSION['user']['sexual']			= 	$row['user']['sexual'];
		$_SESSION['user']['lifestyle']		= 	$row['user']['lifestyle'];

		return $this;
	}
	function getUser($id) {
		$data=new database();	
		$data->db(DATABASE)->collection('users');
		$data->criteria(array('_id'=>new MongoId($id)));
		$row=$data->findOne();	
		$data->close();
		return $row;
	}


}

?>